package com.snail.handler;

import com.snail.VO.ResultVO;
import com.snail.config.ProjectUrlConfig;
import com.snail.exception.ResponseBankException;
import com.snail.exception.SellException;
import com.snail.exception.SellerAuthorizeException;
import com.snail.utils.ResultVOUtil;
import org.springframework.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author 【swg】.
 * @Date 2017/12/11 21:34
 * @DESC
 * @CONTACT 317758022@qq.com
 */
@ControllerAdvice
public class SellExceptionHandler {
    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    @ExceptionHandler(value = SellerAuthorizeException.class)
    public ModelAndView handlerAuthorizeException() {
        return new ModelAndView("redirect:"
                .concat(projectUrlConfig.getSell())
                .concat("/sell/login"));
    }

    /**针对SellException做一个统一异常处理，返回固定的格式ResultVO*/
    @ExceptionHandler(value = SellException.class)
    @ResponseBody
    public ResultVO handlerSellerException(SellException e){
        return ResultVOUtil.error(e.getCode(),e.getMessage());
    }

    /**上面的统一异常虽然可以返回我们需要的信息，但是状态吗仍然是200，这里可以返回想要的状态吗*/
    @ExceptionHandler(value = ResponseBankException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void handlResponseBankException(){

    }
}
